﻿Imports System.Data.SqlClient
Public Class frmPrincipal
    Public NivelSeguridad As Integer
    Public UsuarioLogin As String
    Private Sub btnNuevoCredito_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNuevoCredito.ItemClick
        Dim nuevoCredito As New frmNuevoCredito
        nuevoCredito.MdiParent = Me
        nuevoCredito.Show()
    End Sub

    Private Sub btnActualizarPagos_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnActualizarPagos.ItemClick
        'Dim creditosVigentes As New frmControlCreditosVigentes
        frmControlCreditosVigentes.MdiParent = Me
        frmControlCreditosVigentes.Show()
    End Sub

    Private Sub btnVerMoras_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnVerMoras.ItemClick
        Dim creditosMora As New frmMonitoreoCreditos
        creditosMora.MdiParent = Me
        creditosMora.Show()
    End Sub

    Private Sub btnCalculoPlanPago_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCalculoPlanPago.ItemClick
        Dim calculoPlan As New fmrNuevoPlanVentas
        calculoPlan.MdiParent = Me
        calculoPlan.Show()
    End Sub

    Private Sub btnAdminsitrarOficiales_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnAdminsitrarOficiales.ItemClick
        Dim admonOficiales As New frmOficialesCredito
        admonOficiales.MdiParent = Me
        admonOficiales.Show()
    End Sub

    Private Sub frmPrincipal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If NivelSeguridad = 1 Then
            btnNuevoCredito.Enabled = True
            btnAdminsitrarOficiales.Enabled = True
            btnReporteGeneralResumido.Enabled = True
            btnHistorialCreditosCliente.Enabled = True
            btnAdministrarVendedores.Enabled = True
            btnReversiones.Enabled = True
            btnRespaldoDB.Enabled = True
        ElseIf NivelSeguridad = 2 Then
            btnNuevoCredito.Enabled = True
            btnReporteGeneralResumido.Enabled = True
            btnHistorialCreditosCliente.Enabled = True
            btnReversiones.Enabled = True
            btnAdminsitrarOficiales.Enabled = False
        ElseIf NivelSeguridad = 5 Then
            btnActualizarPagos.Enabled = False
            btnVerMoras.Enabled = False
        End If
    End Sub

    Private Sub btnReporteGeneralResumido_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReporteGeneralResumido.ItemClick
        frmReporteGeneralResumido.MdiParent = Me
        frmReporteGeneralResumido.Show()
    End Sub

    Private Sub btnHistorialCreditosCliente_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnHistorialCreditosCliente.ItemClick
        frmComportamientoCliente.MdiParent = Me
        frmComportamientoCliente.Show()
    End Sub

    Private Sub btnSalir_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSalir.ItemClick
        Application.Exit()
    End Sub

    Private Sub btnAdministrarVendedores_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnAdministrarVendedores.ItemClick
        frmAdministrarVendedores.MdiParent = Me
        frmAdministrarVendedores.Show()
    End Sub

    Private Sub btnReversiones_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReversiones.ItemClick
        frmReporteReversiones.MdiParent = Me
        frmReporteReversiones.Show()
    End Sub

    Private Sub btnRespaldoDB_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnRespaldoDB.ItemClick
        GenerarRespaldoSoftTechDB()
    End Sub

    Private Sub GenerarRespaldoSoftTechDB()
        If varConexionMegaStore.State = ConnectionState.Open Then
            varConexionMegaStore.Close()
        End If
        Try
            Dim existeCarpeta As Boolean
            existeCarpeta = System.IO.Directory.Exists("C:\BackupCreditos")
            If existeCarpeta = False Then
                System.IO.Directory.CreateDirectory("C:\BackupCreditos")
            End If
            Dim sqlGenerarRespaldo As String
            varConexionMegaStore.Open()
            sqlGenerarRespaldo = "BACKUP DATABASE MEGASTORE TO "
            sqlGenerarRespaldo += "DISK = N'C:\BackupCreditos\MEGASTORE_" + Date.Now.Day.ToString + "-" + Date.Now.Month.ToString + "-" + Date.Now.Year.ToString + ".bak' WITH INIT, "
            sqlGenerarRespaldo += "NOUNLOAD, NAME = N'Respaldo de MEGASTORE', NOSKIP, "
            sqlGenerarRespaldo += "STATS = 10, NOFORMAT"

            Dim cmdGenerarRespaldo As New SqlCommand(sqlGenerarRespaldo, varConexionMegaStore)
            cmdGenerarRespaldo.ExecuteNonQuery()

            varConexionMegaStore.Close()

            MessageBox.Show("Se ha generado correctamente el archivo de respaldo.", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frmPrincipal_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Application.Exit()
    End Sub

    Private Sub btnTotalizacionDeuda_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTotalizacionDeuda.ItemClick
        frmTotalizacionDeudaCliente.MdiParent = Me
        frmTotalizacionDeudaCliente.Show()
    End Sub
End Class